package com.dmooo.yxmmerchant.view.ui;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.common.glide.GlideUtils;
import com.common.net.entity.SubjectPost;
import com.common.net.listener.HttpOnNextListener;
import com.common.net.subscribers.ProgressSubscriber;
import com.common.util.RecycleViewDivider;
import com.common.util.ScreenUtil;
import com.dmooo.yxmmerchant.R;
import com.dmooo.yxmmerchant.adapter.MyGoodsOrderAdapter;
import com.dmooo.yxmmerchant.adapter.MyServiceOrderAdapter;
import com.dmooo.yxmmerchant.base.BaseActivity;
import com.dmooo.yxmmerchant.common.Config;
import com.dmooo.yxmmerchant.domain.ExpressDetailBean;
import com.dmooo.yxmmerchant.domain.GoodsOrderDetailBean;
import com.dmooo.yxmmerchant.domain.GoodsOrderListBean;
import com.dmooo.yxmmerchant.domain.LogisticsListBean;
import com.dmooo.yxmmerchant.domain.ServiceOrderDetailBean;
import com.dmooo.yxmmerchant.domain.ServiceOrderListBean;
import com.dmooo.yxmmerchant.fragment.contract.GoodsOrderContract;
import com.dmooo.yxmmerchant.fragment.contract.ServiceOrderContract;
import com.dmooo.yxmmerchant.http.HttpManager;
import com.google.gson.GsonBuilder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class UserInfoActivity extends BaseActivity implements OnRefreshLoadMoreListener, GoodsOrderContract.GoodsOrderView {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.img_head)
    ImageView imgHead;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_desc)
    TextView txtDesc;
    @BindView(R.id.rg_mall)
    RadioGroup rgMall;
    @BindView(R.id.recyclerview_goods)
    RecyclerView recyclerviewGoods;
    @BindView(R.id.recyclerview_service)
    RecyclerView recyclerviewService;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    GsonBuilder builder = new GsonBuilder();
    private MyGoodsOrderAdapter myGoodsOrderAdapter;
    private List<GoodsOrderListBean.Item> list = new ArrayList<>();
    int page1 = 1, page2;
    private MyServiceOrderAdapter myServiceOrderAdapter;
    private List<ServiceOrderListBean.Item> listService = new ArrayList<>();

    @Override
    protected int getLayout() {
        return R.layout.activity_userinfo;
    }
    private class MyView implements ServiceOrderContract.ServiceOrderView{

        @Override
        public void showUserOrderList(List<ServiceOrderListBean.Item> items) {
            if (page2 == 1) {
            listService.clear();
        }
        listService.addAll(items);
        myServiceOrderAdapter.notifyDataSetChanged();
        refresh.finishRefresh();
        refresh.finishLoadMore();
        }

        @Override
        public void cancleSuccess(int pos) {

        }

        @Override
        public void applyBackMoneySuccess() {

        }

        @Override
        public void showLogisticsList(List<LogisticsListBean.Item> items) {

        }

        @Override
        public void fillInRefundExpressNumSuccess(int pos) {

        }

        @Override
        public void showExpressList(List<ExpressDetailBean.Item.Child> items) {

        }

        @Override
        public void checkBackMoneySuccess() {

        }

        @Override
        public void confirmOrderSuccess(int pos) {

        }

        @Override
        public void showOrderDetail(ServiceOrderDetailBean orderDetailBean) {

        }

        @Override
        public void showProgress() {

        }

        @Override
        public void hideProgress() {

        }

        @Override
        public void errorMsg(String msg) {

        }
    }
    @Override
    protected void initEventAndData() {
        tvTitle.setText("客户基本信息");
        builder.serializeNulls();
        Config.uid=getIntent().getExtras().getString("uid");
        getInfo();
        myGoodsOrderAdapter = new MyGoodsOrderAdapter(R.layout.item_my_order, list);
        recyclerviewGoods.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerviewGoods.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, ScreenUtil.dp2px(this, 10), getResources().getColor(R.color.col_f0)));
        recyclerviewGoods.setAdapter(myGoodsOrderAdapter);
        refresh.setOnRefreshLoadMoreListener(this);

        myServiceOrderAdapter = new MyServiceOrderAdapter(R.layout.item_my_order, listService);
        recyclerviewService.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerviewService.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, ScreenUtil.dp2px(this, 10), getResources().getColor(R.color.col_f0)));
        recyclerviewService.setAdapter(myServiceOrderAdapter);
        rgMall.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_one) {
                    recyclerviewGoods.setVisibility(View.VISIBLE);
                    recyclerviewService.setVisibility(View.GONE);
                } else {
                    recyclerviewGoods.setVisibility(View.GONE);
                    recyclerviewService.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void getInfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", getIntent().getExtras().getString("uid"));
        SubjectPost post = new SubjectPost(new ProgressSubscriber(new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                try {
                    JSONObject obj = new JSONObject(builder.create().toJson(o));
                    txtName.setText(obj.getJSONObject("user_detail").getString("nickname") == null ? obj.getJSONObject("user_msg").getString("phone") : obj.getJSONObject("user_detail").getString("nickname"));
                    GlideUtils.showImageViewToCircle(UserInfoActivity.this, getResources().getDrawable(R.mipmap.defaultpicture_pc), obj.getJSONObject("user_detail").getString("avatar"), imgHead);
                    txtDesc.setText(obj.getJSONObject("user_msg").getString("id_name"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(Object o) {

            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        }, this, true, false, "删除中"), map);
        HttpManager.getInstance().getMsg(post);
    }

    @Override
    protected void createPresenter() {

    }

    @OnClick(R.id.tv_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        if (recyclerviewGoods.getVisibility() == View.VISIBLE) {
            page1++;
        } else {
            page2++;
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        if (recyclerviewGoods.getVisibility() == View.VISIBLE) {
            page1 = 1;
        } else {
            page2 = 1;
        }
    }

    @Override
    public void showUserOrderList(List<GoodsOrderListBean.Item> items) {
        if (page1 == 1) {
            list.clear();
        }
        list.addAll(items);
        myGoodsOrderAdapter.notifyDataSetChanged();
        refresh.finishRefresh();
        refresh.finishLoadMore();
    }
//
//    @Override
//    public void showUserOrderList(List<ServiceOrderListBean.Item> items) {
//        if (page2 == 1) {
//            listService.clear();
//        }
//        listService.addAll(items);
//        myServiceOrderAdapter.notifyDataSetChanged();
//        refresh.finishRefresh();
//        refresh.finishLoadMore();
//    }
    @Override
    public void applyBackMoneySuccess() {

    }

    @Override
    public void showLogisticsList(List<LogisticsListBean.Item> items) {

    }

    @Override
    public void fillInRefundExpressNumSuccess() {

    }

    @Override
    public void showExpressList(List<ExpressDetailBean.Item.Child> items) {

    }

    @Override
    public void checkBackMoneySuccess() {

    }

    @Override
    public void backMoneySuccess() {

    }

    @Override
    public void confirmSuccess(int pos) {

    }

    @Override
    public void showOrderDetail(GoodsOrderDetailBean orderDetailBean) {

    }
}
